package com.deer.backup.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.deer.backup.repository.BackUpRepository;
import com.deer.backup.result.PageDomain;
import com.deer.backup.vo.BackUp;

@Service
@Transactional
public class BackUpService {

    @Autowired
    private BackUpRepository backUpRepository;

    // 分页查询所有BackUp数据
    public Page<BackUp> findBackUp(PageDomain page) {
    	//排序
    	Sort sort = Sort.by(Sort.Direction.DESC, "id");
    	Pageable pageable = PageRequest.of(page.getPage()-1, page.getLimit(),sort);
    	return backUpRepository.findAll(pageable);
    }
    
    // 查询所有BackUp数据
    public List<BackUp> findBackUp() {
    	return backUpRepository.findAll();
    }

    // 根据ID查询单个BackUp数据
    public BackUp getBackUpById(Long id) {
        Assert.notNull(id, "ID不能为null");
        return backUpRepository.findById(id).orElse(null);
    }

    // 添加新的BackUp数据
    public void addBackUp(BackUp backUp) {
        Assert.notNull(backUp, "BackUp对象不能为null");
        backUpRepository.save(backUp);
    }

    // 更新指定ID的BackUp数据
    public BackUp updateBackUp(BackUp backUp) {
        Assert.notNull(backUp, "BackUp对象不能为null");
        return backUpRepository.save(backUp);
    }

    // 删除指定ID的BackUp数据
    public void deleteBackUpById(Long id) {
        Assert.notNull(id, "ID不能为null");
        backUpRepository.deleteById(id);
    }
}